#include <stdafx.h>

#include <string>
#include <iostream>
#include "SLHead.h"

///////////    CSLHead      /////////////////////////
CSLHead::CSLHead()
{
	m_nID = -1;
	m_nWidth = 0;
	m_strDescription = _T("");
	m_strName = _T("");
}

///////////    CSLHeadContainer      /////////////////////////
CSLHeadContainer* CSLHeadContainer::m_pInstance = 0;

CSLHeadContainer* CSLHeadContainer::GetInstance()
{
	if(m_pInstance == 0)
	{
		m_pInstance = new CSLHeadContainer;
	}

	return m_pInstance;
}

CSLHeadContainer::CSLHeadContainer() 
{
	LoadSLHeads(_T("StockListColumns.txt"));
}

CSLHeadContainer::~CSLHeadContainer()
{
	if(m_pInstance != 0)
	{
		StoreSLHeads(_T("StockListColumns.txt"));
		
		delete m_pInstance;
		m_pInstance = 0;
	}
}

void CSLHeadContainer::LoadSLHeads(const CString& strFileName)
{
	std::ifstream ifs((LPCSTR)strFileName);
	if(! ifs) { return; }

	int nCount;
	ifs >> nCount;

	for(int i = 0; i < nCount; ++i)
	{
		std::string strDescriptionTemp;
		std::string strNameTemp;

		CSLHead slh;
		ifs >> slh.m_nID 
			>> strDescriptionTemp 
			>> slh.m_nWidth 
			>> strNameTemp;
		slh.m_strDescription = CString(strDescriptionTemp.c_str());
		slh.m_strName = CString(strNameTemp.c_str());

		m_mapSLHeads[slh.m_nID] = slh;
	}
}

void CSLHeadContainer::StoreSLHeads(const CString& strFileName)
{

}

void CSLHeadContainer::Add(const CSLHead& slh)
{
	std::map<int, CSLHead>::iterator it = m_mapSLHeads.find(slh.m_nID);
	_ASSERT(it == m_mapSLHeads.end());

	m_mapSLHeads[slh.m_nID] = slh;
}

void CSLHeadContainer::Remove(const int nID)
{
	std::map<int, CSLHead>::iterator it = m_mapSLHeads.find(nID);
	m_mapSLHeads.erase(it);
}

CSLHead CSLHeadContainer::Get(const int nID)
{
	std::map<int, CSLHead>::iterator it = m_mapSLHeads.find(nID);
	if(it != m_mapSLHeads.end())
	{
		return it->second;
	}
	else
	{
		return CSLHead();
	}
}

void CSLHeadContainer::Set(const CSLHead& slh)
{
	m_mapSLHeads[slh.m_nID] = slh;
}

bool CSLHeadContainer::Find(const int nID)
{
	return m_mapSLHeads.find(nID) != m_mapSLHeads.end();
}

int CSLHeadContainer::Size()
{
	return (int)m_mapSLHeads.size();
}

///////////    CSLColums      /////////////////////////
CSLColumns* CSLColumns::m_pInstance = 0;

CSLColumns* CSLColumns::GetInstance()
{
	if(m_pInstance == 0)
	{
		m_pInstance = new CSLColumns;
	}
	return m_pInstance;
}

CSLColumns::CSLColumns()
{
	LoadColumns(_T("input.txt"));
}

CSLColumns::~CSLColumns()
{
	if(m_pInstance != 0)
	{
		StoreColumns(_T("input.txt"));
		delete m_pInstance;
		m_pInstance = 0;
	}
}

void CSLColumns::LoadColumns(const CString& strFileName)
{
	std::ifstream ifs((LPCTSTR)strFileName);
	if(! ifs) { return; }

	int nColCount;
	ifs >> nColCount;

	for(int i = 0; i < nColCount; ++i)
	{
		int nHeadID;
		ifs >> nHeadID;
		m_vecSLHeadIDs.push_back(nHeadID); 
	}
}

void CSLColumns::StoreColumns(const CString& strFileName)
{

}

int CSLColumns::Size()
{
	return (int)m_vecSLHeadIDs.size();
}

void CSLColumns::Insert(const int nColNo, const int nHeadID)
{
	int nColCount = (int)Size();
	_ASSERT( nColNo >= 0 && nColNo <= (nColCount+1) );
	_ASSERT( CSLHeadContainer::GetInstance()->Find(nHeadID) == true );

	m_vecSLHeadIDs.insert(m_vecSLHeadIDs.begin() + nColNo, nHeadID);
}

void CSLColumns::Remove(const int nColNo)
{
	int nColCount = (int)Size();
	_ASSERT( nColNo >= 0 && nColNo <= nColCount );

	m_vecSLHeadIDs.erase(m_vecSLHeadIDs.begin() + nColNo);
}

void CSLColumns::Set(const int nColNo, const int nHeadID)
{
	int nColCount = (int)Size();
	_ASSERT( nColNo >= 0 && nColNo <= nColCount );
	_ASSERT( CSLHeadContainer::GetInstance()->Find(nHeadID) == true );

	m_vecSLHeadIDs[nColNo] = nHeadID;
}

CSLHead CSLColumns::Get(const int nColNo)
{
	int nColCount = (int)Size();
	_ASSERT( nColNo >= 0 && nColNo <= nColCount );

	int nHeadID = m_vecSLHeadIDs[nColNo];
	return CSLHeadContainer::GetInstance()->Get(nHeadID);
}

void CSLColumns::Display()
{
	int n = (int)Size();
	std::cout << n << std::endl;
	for(int i = 0; i < n; ++i)
	{
		CSLHead slh = Get(i);
		std::cout << slh.m_nID << _T(" ")
			<< slh.m_strDescription << _T(" ")
			<< slh.m_nWidth << _T(" ")
			<< slh.m_strName 
			<< std::endl;
	}
}